<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btncancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in getRoleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnok">确定</el-button>
        <el-button size="small" @click="btncancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { roleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
      // default: null
    }
  },
  data() {
    return {
      // 全部角色列表
      getRoleList: [],
      // 点击的角色
      checkList: []
    }
  },
  created() {
    this.roleList()
  },
  methods: {
    async roleList() {
      const { rows } = await roleList()
      console.log(rows)
      this.getRoleList = rows
    },
    async btnok() {
      // 发送请求
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      // 提示用户
      this.$message.success('分配成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btncancel() {
      this.$emit('update:showDialog', false)
    },
    // 根据id获取角色详细信息
    async getCheckedRole(id) {
      // const res = await getUserDetailById(id)
      const { roleIds } = await getUserDetailById(id)
      // this.checkList = res
      // .console.log(256, this.checkList)
      this.checkList = roleIds
    }
  }
}
</script>

<style>

</style>
